package cate.game.role.mall.msg;

import cate.common.annotation.OperationSetter;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBMall;
import cate.game.res.MixResIo;
import cate.game.role.Role;
import cate.game.stat.shop.ShopBuyAction;
import easy.java.dev.note.NoteField;
import easy.java.struct.ref.IntRef;

@ClientMsg(title = "购买商品 の 请求")
@OperationSetter(value = GDOperation.MALL_BUY, sub = "mallId")
public class BuyItemsReq implements MsgInput {
    @NoteField(value = "商店ID")
    public int mallId;

    @NoteField(value = "需要购买商品的唯一id", detail = "现在同一个商品可能会刷出多次")
    public String uid;

    @NoteField(value = "购买商品的数量")
    public int count;

    @Override
    public MsgConfig conf() {
        return MsgIdBMall.BUY_ITEMS_REQ;
    }

    @Override
    public void execute(GameBody game, ClientContext cc) {
        Role role = cc.getRole();
        IntRef mallItemIdFill = new IntRef();
        GameResult<MixResIo> r = role.getMall().buyItem(mallId, uid, count, mallItemIdFill);
        if(!r.ok()){
            game.notice.message(role, r.message);
        } else {
            ShopBuyAction.build(role).mallId(mallId).mallItemId(mallItemIdFill.value()).mallItemNum(count).io(r.data).send();
        }
    }
}
